﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebChamCong
{
    public partial class InforEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                load_AcNo();
                Load_Departments();
                Load_Infor(int.Parse(Dll_ACNO.Items[0].Value));
                DTO.NhanVienDTO nv = BUS.NhanVienBUS.GetNhanViensByACNO(int.Parse(Dll_ACNO.Items[0].Value));
                Dll_Department.SelectedValue = nv.DepartmentId.ToString();
            }
        }
        protected void Load_Infor(int ACNOID)
        {
            DTO.NhanVienDTO nv = BUS.NhanVienBUS.GetNhanViensByACNO(ACNOID);
            
                this.ShortName.Text = nv.Name;
                this.TxtFullName.Text = nv.FullName;
                this.txtAddress1.Text = nv.PermantResidance;
                this.TxtAddress2.Text = nv.TemporaryResidance;
                this.TxtCMND.Text = nv.CMND;
                this.datebirth.Text = Convert.ToString(nv.Birthday);
                this.dateCMND.Text = Convert.ToString(nv.DateCmnd);
                this.Txtphone.Text = nv.Phone;
                this.cellfone.Text = nv.Telephone;

        }

        protected void load_AcNo()
        {
            this.Dll_ACNO.Items.Clear();
            List<DTO.NhanVienDTO> nvs = BUS.NhanVienBUS.GetNhanViensByFullName();
            foreach (var nv in nvs)
            {
                this.Dll_ACNO.Items.Add(new ListItem(nv.AcNo, nv.UserId.ToString()));
            }
            this.Dll_Department.SelectedIndex = 0;
        }
        protected void Load_Departments()
        {
                this.Dll_Department.Items.Clear();
                List<DTO.BoPhanDTO> bp = BUS.BoPhanBUS.GetBoPhans();
                foreach (var i in bp)
                {
                    this.Dll_Department.Items.Add(new ListItem(i.DepartmentName, i.DepartmentID.ToString()));
                }
                this.Dll_Department.SelectedIndex = 0;
        }

        protected void Dll_ACNO_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_Infor(int.Parse(Dll_ACNO.SelectedValue));
            DTO.NhanVienDTO nv = BUS.NhanVienBUS.GetNhanViensByACNO(int.Parse(Dll_ACNO.SelectedValue));
            Dll_Department.SelectedValue = nv.DepartmentId.ToString();
        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        protected void Btupdate_Click(object sender, EventArgs e)
        {
            if (BUS.NhanVienBUS.UpdateNhanVien(int.Parse(Dll_ACNO.SelectedValue), ShortName.Text, TxtFullName.Text, Convert.ToDateTime(datebirth.Text), TxtCMND.Text, Convert.ToDateTime(dateCMND.Text), txtAddress1.Text, TxtAddress2.Text, Txtphone.Text, cellfone.Text, Email.Text, Convert.ToInt32(Dll_Department.SelectedValue), txttrinhdo.Text))
            {
                DateTime begintime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 12, 0, 0);
                DateTime endtime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month), 12, 0, 0);
                DTO.NhanVienDTO nv = BUS.NhanVienBUS.GetNhanViensByACNO(int.Parse(Dll_ACNO.SelectedValue));
                if (BUS.LichBUS.TaoPhanCa(nv.UserId, begintime, endtime))
                {
                    MessageBox("Cập nhật Thành Công !");
                }
                else
                {
                    MessageBox("Cập nhật không thành công  !");
                }
            }
        }
        
    }
}